package info.liujiachen.security.test.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

@Component
public class MD5Util {

    public static String salt;

    @Value("${md5.salt}")
    public void setSalt(String salt) {
        MD5Util.salt = salt;
    }

    public static String md5(String src){
        if (StringUtils.isNoneBlank(src)) {
            return DigestUtils.md5DigestAsHex(src.getBytes());
        } else {
            return null;
        }
    }

    public static String addSalt(String src) {
        return addSalt(src, MD5Util.salt);
    }

    public static String addSalt(String src, String salt) {
        return md5(src + md5(salt));
    }

    public static String doubleAddSalt(String src) {
        String formPass = addSalt(src);
        return addSalt(formPass);
    }

    public static String doubleAddSalt(String src, String salt) {
        String formPass = addSalt(src, salt);
        return addSalt(formPass, salt);
    }
}
